﻿using SuperSocket;
using SuperSocket.ProtoBase;
using SuperSocket.Command;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;

namespace GpsServer
{
    [Command(Key = ProtocolNo.LOGIN)]
    public class LoginCommand : IAsyncCommand<MyPackageInfo>
    {
        private readonly ILogger _logger;

        public LoginCommand(ILogger<LoginCommand> logger)
        {
            _logger = logger;
        }

        public async ValueTask ExecuteAsync(IAppSession session, MyPackageInfo package)
        {
            if (package.Content.Length < 1) return;

            //终端ID
            var sb = new StringBuilder(16);
            foreach (var a in package.Content)
            {
                sb.AppendFormat("{0:x2}", a);
            }
            var terminalID = sb.ToString().TrimStart('0');
            var mySession = ((MySession)session);
            mySession.TerminalID = terminalID.ToString();
            mySession.IsLogin = true;

            _logger.LogDebug("LOGIN: TerminalID={0}", terminalID);

            //响应
            var data = Helper.StringToHexByte("787805010001D9DC0D0A");
            await session.SendAsync(data);

        }
    }
}